require 'fileutils'
desc "Loads the fixtures and creates the needed files."
task :load_tests => ["db:fixtures:load", "environment"] do
  contest = Contest.find(:all)
  contest.each{|c| c.synchronize}
  problems = Problem.find(:all)
  problems.each do |p|
    p.synchronize
    dir = p.directory
    config = YAML.load_file(File.join("config","configuration.yml"))
    path = File.join(File.join(File.join(File.join(config['base_dir'],'test'),'files'),p.contest.set_code),p.code_name)
    if !FileTest.exists?(File.join(dir,config['answer_file'])) && FileTest.exists?(File.join(path,config['answer_file']))
      FileUtils.cp(File.join(path,config['answer_file']),File.join(dir,config['answer_file']))
    end
    if !FileTest.exists?(File.join(dir,config['test_file'])) && FileTest.exists?(File.join(path,config['test_file']))
      FileUtils.cp(File.join(path,config['test_file']),File.join(dir,config['test_file']))
    end
    if !FileTest.exists?(File.join(dir,config['checker_name'])) && FileTest.exists?(File.join(path,config['checker_name']))
      FileUtils.cp(File.join(path,config['checker_name']),File.join(dir,config['checker_name']))
    end
    if !FileTest.exists?(File.join(dir,"description.doc")) && FileTest.exists?(File.join(path,"description.doc"))
      FileUtils.cp(File.join(path,"description.doc"),File.join(dir,"description.doc"))
    elsif !FileTest.exists?(File.join(dir,"description.pdf")) && FileTest.exists?(File.join(path,"description.pdf"))
      FileUtils.cp(File.join(path,"description.pdf"),File.join(dir,"description.pdf"))
    end
  end
end